import scrapy
from nubiantokyo.items import NubiantokyoItem
import re


class GoodsSpider(scrapy.Spider):
    name = 'goods'
    allowed_domains = ['nubiantokyo.com']
    start_urls = ['https://nubiantokyo.com/collections/all-items/']

    def parse(self, response):
        for category in response.css('li.site-nav__item')[3].css('ul.site-nav__dropdown li'):
            category_name = category.css('a::text').get().strip()
            href = category.css('a').attrib['href']
            yield scrapy.Request(response.urljoin(href), self.parse_goods_list,meta={'category_name': category_name})

    def parse_goods_list(self, response):
        if response.css('.pagination .next'):
            next_page = response.css('.pagination .next a').attrib['href']
            self.logger.info(next_page)
            yield scrapy.Request(response.urljoin(next_page), self.parse_goods_list,meta=response.meta)

        for goods in response.css('.grid-product__link'):
            details_url = goods.attrib['href']
            yield scrapy.Request(response.urljoin(details_url), self.parse_goods_details,meta=response.meta,dont_filter=True)

    def parse_goods_details(self, response):
        product_name = response.css('.product-single__title::text').get()
        self.logger.info('产品名称:' + product_name)
        product_price = response.css('.product__price .money::text').get().replace(',', '').replace('¥', '')
        self.logger.info('价格:' + product_price)
        product_images = response.css('.product__thumb-item a::attr(href)').getall()
        if len(product_images) == 0:
            product_images = response.css('.product-image-main img::attr(data-photoswipe-src)').getall()
        content = ''

        item = NubiantokyoItem()
        item['name'] = product_name.strip()
        if response.css('.womens__badge__product') is None:
            item['category_name'] = response.meta['category_name']
        else:
            item['category_name'] = response.meta['category_name']+'|WOMAN'
        item['source'] = response.url

        def imgs_handler(url):
            if url.startswith('//'):
                return 'https:'+url
            return url

        product_images = list(map(imgs_handler, product_images))
        item['image_urls'] = product_images
        item['thumbs'] = product_images[0]
        item['pictures'] = ','.join(product_images)
        item['content'] = content
        item['specs'] = []
        for i, spec in enumerate(response.css('.variant-input')):
            spec_name = spec.css('label::text').get()
            spec_num = 0
            if spec.css('label.disabled').get() is None:
                spec_num = 1
            item['specs'].append({
                'spec_id': i+1,
                'spec_name': spec_name,
                'spec_num': spec_num,
                'price': product_price,
                'thumb': product_images[0],
            })
        yield item
